#!/bin/sh
# PCP QA Test No. 1906
# Testing pmseries language queries involving scalar operations
#
# Copyright (c) 2022 Shiyao Chen.  All Rights Reserved.
#

seq=`basename $0`
echo "QA output created by $seq"
path=""

# get standard environment, filters and checks
. ./common.product
. ./common.filter
. ./common.check

# This test is not run if we dont have pmseries and a key server installed.
_check_series

_cleanup()
{
    [ -n "$key_server_port" ] && $keys_cli -p $key_server_port shutdown
    _restore_config $PCP_SYSCONF_DIR/pmseries
    cd $here
    $sudo rm -rf $tmp $tmp.*
}

status=1	# failure is the default!
trap "_cleanup; exit \$status" 0 1 2 3 15

_filter_source()
{
    sed \
	-e "s,$here,PATH,g" \
    #end
}

_sort_context()
{
    sed -e 's/    Context: //g' -e 's/, /\n/g' | \
    LC_COLLATE=POSIX sort | \
    $PCP_AWK_PROG 'BEGIN { printf "    Context: " } {
	if (count == 0) {printf("%s", $0)} else {printf(", %s", $0)};
	count++
    } END { print out }'
}

# real QA test starts here
key_server_port=`_find_free_port`
_save_config $PCP_SYSCONF_DIR/pmseries
$sudo rm -f $PCP_SYSCONF_DIR/pmseries/*

echo "Start test key server ..."
$key_server --port $key_server_port --save "" > $tmp.keys 2>&1 &
_check_key_server_ping $key_server_port
_check_key_server $key_server_port
echo

_check_key_server_version $key_server_port

args="-p $key_server_port -Z UTC"

echo "== Load metric data into this key server instance"
pmseries $args --load "{source.path: \"$here/archives/proc\"}" | _filter_source


echo;echo "== Verify multiplication operator for a non-singular metric"
pmseries $args 'kernel.all.load[count:5]'
echo "Verify multiplication of functions"
pmseries $args 'max(kernel.all.load[count:5]) * max(kernel.all.load[count:5])'
echo "Verify multiplication of expressions"
pmseries $args 'kernel.all.load[count:5] * kernel.all.load[count:5]'
echo "Verify multiplication between expressions and functions"
pmseries $args 'abs(kernel.all.load[count:5]) * kernel.all.load[count:5]'
pmseries $args 'kernel.all.load[count:5] * abs(kernel.all.load[count:5])'
pmseries $args 'kernel.all.load[count:5] * log(kernel.all.load[count:5])'
echo "Verify scalar multiplication"
pmseries $args 'kernel.all.load[count:5] * 2'
pmseries $args '2 * kernel.all.load[count:5]'
pmseries $args 'abs(kernel.all.load[count:5]) * 2'
pmseries $args '2 * abs(kernel.all.load[count:5])'
pmseries $args '0 * kernel.all.load[count:5]'
pmseries $args '1.1 * kernel.all.load[count:5]'
pmseries $args 'kernel.all.load[count:5] * 1.5'


echo;echo "== Verify multiplication operator for a non-singular metric, only one instance"
pmseries $args 'kernel.all.load{instance.name == "5 minute"}[count:5]'
echo "Verify multiplication of functions"
pmseries $args 'max(kernel.all.load{instance.name == "5 minute"}[count:5]) * max(kernel.all.load{instance.name == "5 minute"}[count:5])'
echo "Verify multiplication of expressions"
pmseries $args 'kernel.all.load{instance.name == "5 minute"}[count:5] * kernel.all.load{instance.name == "5 minute"}[count:5]'
echo "Verify multiplication between expressions and functions"
pmseries $args 'abs(kernel.all.load{instance.name == "5 minute"}[count:5]) * kernel.all.load{instance.name == "5 minute"}[count:5]'
pmseries $args 'kernel.all.load{instance.name == "5 minute"}[count:5] * abs(kernel.all.load{instance.name == "5 minute"}[count:5])'
pmseries $args 'kernel.all.load{instance.name == "5 minute"}[count:5] * log(kernel.all.load{instance.name == "5 minute"}[count:5])'
echo "Verify scalar multiplication"
pmseries $args 'kernel.all.load{instance.name == "5 minute"}[count:5] * 2'
pmseries $args '2 * kernel.all.load{instance.name == "5 minute"}[count:5]'
pmseries $args 'abs(kernel.all.load{instance.name == "5 minute"}[count:5]) * 2'
pmseries $args '2 * abs(kernel.all.load{instance.name == "5 minute"}[count:5])'
pmseries $args '0 * kernel.all.load{instance.name == "5 minute"}[count:5]'
pmseries $args '1.1 * kernel.all.load{instance.name == "5 minute"}[count:5]'
pmseries $args 'kernel.all.load{instance.name == "5 minute"}[count:5] * 1.5'

echo;echo "== Verify multiplication operator for a singular metric"
pmseries $args 'kernel.all.uptime[count:10]'
echo "Verify multiplication of functions"
pmseries $args 'max(kernel.all.uptime[count:10]) * max(kernel.all.uptime[count:10])'
echo "Verify multiplication of expressions"
pmseries $args 'kernel.all.uptime[count:10] * kernel.all.uptime[count:10]'
echo "Verify multiplication between expressions and functions"
pmseries $args 'abs(kernel.all.uptime[count:10]) * kernel.all.uptime[count:10]'
pmseries $args 'kernel.all.uptime[count:10] * abs(kernel.all.uptime[count:10])'
pmseries $args 'kernel.all.uptime[count:10] * log(kernel.all.uptime[count:10])'
echo "Verify scalar multiplication"
pmseries $args 'kernel.all.uptime[count:10] * 2'
pmseries $args '2 * kernel.all.uptime[count:10]'
pmseries $args 'abs(kernel.all.uptime[count:10]) * 2'
pmseries $args '2 * abs(kernel.all.uptime[count:10])'
pmseries $args '0 * kernel.all.uptime[count:10]'
pmseries $args '1.1 * kernel.all.uptime[count:10]'
pmseries $args 'kernel.all.uptime[count:10] * 1.5'


# success, all done
status=0
exit
